// 课程数据结构
const knowledgeMapData = {
    name: '课程核心',
    children: [
        {
            name: '作战运用',
            children: [
                { 
                    name: '阵地优化',
                },
                {
                    name: '全功能运用',
                },
                {
                    name: '状态检查评估',
                }
            ]
        },
        {
            name: '复杂战场环境应对',
            children: [
                // { 
                //     name: '故障维修',
                // },
                {
                    name: '架设撤收',
                },
                {
                    name: '终端操作',
                },
                {
                    name: '工作原理'
                }
            ]
        },
        {
            name: '组网协同',
            children: [
                { 
                    name: '监控操作',
                },
                {
                    name: '全功能运用',
                }
            ]
        },
        {
            name: '应急处置',
            children: [
                { 
                    name: '故障维修',
                },
                {
                    name: '日常维护',
                },
                {
                    name: '开关机',
                }
            ]
        },
    ]
};

const notShowDepth = 4;

// 转换数据结构为ECharts需要的格式
const nodes = [];
const links = [];
let index = 0;

function processNode(node, parentId = null, depth = 0) {
    const nodeId = index++;
    nodes.push({
        id: nodeId,
        name: node.name,
        symbolSize: 70 - depth * 5,
        itemStyle: {
            color: [
                '#5470c6',  // 课程总览
                '#91cc75',  // 大模块
                '#fac858',  // 子模块
                '#ee6666'   // 知识点
            ][depth]
        },
        label: {
            show: depth < notShowDepth,
            fontSize: 25 - depth * 2,
            color: '#fff',
        },
        depth: depth
    });

    if (parentId !== null) {
        links.push({
            source: parentId,
            target: nodeId,
            lineStyle: {
                color: '#a3a3a3',
                curveness: depth === notShowDepth ? 0.3 : 0
            }
        });
    }

    if (node.children) {
        node.children.forEach(child => {
            processNode(child, nodeId, depth + 1);
        });
    }
}

processNode(knowledgeMapData);
// console.log(nodes)
const option = {
    tooltip: {},
    legend: {
        data: ['课程总览', '大模块', '子模块', '知识点']
    },
    animationDurationUpdate: 1500,
    animationEasingUpdate: 'quinticInOut',
    series: [{
        type: 'graph',
        layout: 'force',
        force: {
            repulsion: 200,
            edgeLength: [50, 150]
        },
        data: nodes,
        links: links,
        roam: true,
        focusNodeAdjacency: true,
        lineStyle: {
            width: 2,
            opacity: 0.8
        },
        emphasis: {
            label: {
                show: true,
                fontSize: 16
            },
            lineStyle: {
                width: 3
            }
        }
    }]
};

document.addEventListener('DOMContentLoaded', () => {
    const chart = echarts.init(document.getElementById('knowledgemap'));
    chart.setOption(option);

    // 窗口自适应
    window.addEventListener('resize', () => chart.resize());
});


